package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : [wangtao]
 * @className : GlobalExection
 * @description : [异常处理机制]
 * @createTime : [2022/10/7 15:11]
 * @updateTime : [2022/10/7 15:11]
 */
//@RestControllerAdvice(annotations = RestController.class)
@Slf4j
@RestControllerAdvice
public class GlobalExection {
    @ExceptionHandler(DuplicateKeyException.class)
    public R<String> handler(DuplicateKeyException ex) {

        String[] s = ex.getMessage().split(" ");
        String str = s[9].replaceAll("'", "");
        if (ex.getMessage().contains("Duplicate entry")) {
            log.info(str + "已存在");

            return R.error(str + "账户已被使用,请重新添加账户");
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(CustomException.class)
    public R<String> customExection(CustomException ex) {
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }

//    @ExceptionHandler(Exception.class)
//    public R<String> globalExection(Exception ex) {
//        log.info(ex.getMessage());
//        return R.error("网络连接慢,请等待");
//    }
}
